package treemodel.container.general;

import document.Context;
import exception.InvalidHtmlDocumentException;
import treemodel.Node;
import treemodel.container.GeneralContainer;
import treemodel.Line;
import treemodel.container.wrapped.ListInstance;
import treeview.NodeView;
import treeview.container.VerticalContainer;
import treeview.unit.EmptyLineView;

/**
 * Created by Pavel Nikolaev
 */
public final class MarkedList extends GeneralContainer {
    private boolean isOrdered;
    private int myCounter;

    public MarkedList(boolean ordered) {
        this.isOrdered = ordered;
        this.myCounter = 0;
    }

    @Override
    public Line createLine() throws InvalidHtmlDocumentException {
        throw new InvalidHtmlDocumentException("Html list doesn't support text placed outside list instance");
    }

    @Override
    public void add(Node node) {
        if (isOrdered) ((ListInstance) node).setMark(++myCounter);
        super.add(node);
    }

    @Override
    public NodeView createView(Context context) {
        if (myNodes.isEmpty()) return new EmptyLineView();
        VerticalContainer container = new VerticalContainer();
        Node node = myNodes.get(0);
        container.add(node.createView(context));
        for (int index = 1; index < myNodes.size(); ++index) {
            node = myNodes.get(index);
            container.add(new EmptyLineView());
            container.add(node.createView(context));
        }
        return container;
    }
}
